<template>
	<el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
		<el-form-item label="标题" prop="title">
			<el-input v-model="form.title"></el-input>
		</el-form-item>
		<el-form-item label="简介" prop="des">
			<el-input v-model="form.des"></el-input>
		</el-form-item>
		<el-form-item label="发布日期" prop="date">
			<el-date-picker type="datetime" v-model="form.publishTime" format="YYYY/MM/DD"
				value-format="x"></el-date-picker>
		</el-form-item>
		<el-form-item label="上传缩略图" prop="thumb">
			<el-upload class="avatar-uploader" drag :action=url :show-file-list="false" :on-success="handleAvatarSuccess">
				<img v-if="thumb" :src="thumb" class="avatar" />
				<el-icon v-else class="avatar-uploader-icon">
					<Plus />
				</el-icon>
			</el-upload>
		</el-form-item>
		<el-form-item label="上传视频" prop="video">
			<el-upload class="avatar-uploader" drag :action=url :show-file-list="false" :on-success="handleVideoSuccess">
				<video v-if="vid" controls :src="vid" class="video"></video>
        <el-icon v-else class="avatar-uploader-icon">
          <Plus />
        </el-icon>
			</el-upload>
		</el-form-item>
		<el-form-item>
			<el-button type="primary" @click="saveEdit(formRef)">保 存</el-button>
		</el-form-item>
	</el-form>
</template>

<script lang="ts" setup>
import { ElMessage, FormInstance, FormRules, UploadProps } from 'element-plus';
import { ref } from 'vue';
import base from '../utils/base';
import video from '../api/video'

const props = defineProps({
	data: {
		type: Object,
		required: true
	},
	edit: {
		type: Boolean,
		required: false
	},
	update: {
		type: Function,
		required: true
	}

});

const url = `${base.rq}/file/upload?token=${localStorage.getItem("token")}`

const defaultData = {
	title: '',
	des: '',
	coverUrl: '',
	resourceUrl: '',
	publishTime: '',

};

const thumb = props.edit ? ref(props.data.coverUrl) : ref('');
const vid = props.edit ? ref(props.data.resourceUrl) : ref('');


const form = ref({ ...(props.edit ? props.data : defaultData) });
// const rules: FormRules = {
// 	name: [{ required: true, message: '用户名', trigger: 'blur' }]
// };
const formRef = ref<FormInstance>();
const saveEdit = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	// formEl.validate(valid => {
	// 	if (!valid) return false;
	// 	props.update(form.value);
	// 	ElMessage.success('保存成功！');
	// });
	commit();
};

const handleAvatarSuccess: UploadProps['onSuccess'] = (response, uploadFile) => {
	// form.value.thumb = URL.createObjectURL(uploadFile.raw!);
	form.value.coverUrl = response.data.realName;
	thumb.value = response.data.tmpUrl;
};

const handleVideoSuccess: UploadProps['onSuccess'] = (response, uploadFile) => {
	form.value.resourceUrl = response.data.realName;
	vid.value = response.data.tmpUrl;
};


const commit = () => {
	video.save(
		form.value,
		(res) => {
			if (res.code === 200) {
				console.log(res)
				ElMessage.success('保存成功！');
				props.update();
			} else {
				ElMessage.success('保存失败！');
				props.update();
			}
		}
	);
}

</script>

<style>
.avatar {
  width: 300px;
}
.avatar-uploader .el-upload {
	border: 1px dashed var(--el-border-color);
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
	transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
	border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
	font-size: 28px;
	color: #8c939d;
	width: 178px;
	height: 178px;
	text-align: center;
}

.video {
	margin-top: 10px;
	width: 100%;
}
</style>
